package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportLqMdsyjcBsInfoBo;
import com.ruoyi.report_lq.domain.vo.ReportLqMdsyjcBsInfoVo;
import com.ruoyi.report_lq.domain.ReportLqMdsyjcBsInfo;
import com.ruoyi.report_lq.mapper.ReportLqMdsyjcBsInfoMapper;
import com.ruoyi.report_lq.service.IReportLqMdsyjcBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * 沥青密度试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-10-30
 */
@RequiredArgsConstructor
@Service
@Transactional
public class ReportLqMdsyjcBsInfoServiceImpl implements IReportLqMdsyjcBsInfoService {

    private final ReportLqMdsyjcBsInfoMapper baseMapper;

    /**
     * 查询沥青密度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportLqMdsyjcBsInfoVo queryById(Long excelId){
        ReportLqMdsyjcBsInfoVo vo = baseMapper.selectVoById(excelId);
        if (Objects.nonNull(vo)){
            setBigDecimalValue(vo);
        }
        return vo;
    }

    private static BigDecimal formatBigDecimal(BigDecimal value) {
        if (value == null) {
            return null;
        }
        DecimalFormat df = new DecimalFormat("0.000");
        String formattedValue = df.format(value);
        return new BigDecimal(formattedValue);
    }

    private static ReportLqMdsyjcBsInfoVo setBigDecimalValue(ReportLqMdsyjcBsInfoVo vo) {
        //b1,c1,d1,e1,b2,c2,d2,e2,c31,c32,d1,e31,e32,f3
        vo.setB1(formatBigDecimal(vo.getB1()));
        vo.setC1(formatBigDecimal(vo.getC1()));
        vo.setD1(formatBigDecimal(vo.getD1()));
        vo.setE1(formatBigDecimal(vo.getE1()));
        vo.setB2(formatBigDecimal(vo.getB2()));
        vo.setC2(formatBigDecimal(vo.getC2()));
        vo.setD2(formatBigDecimal(vo.getD2()));
        vo.setE2(formatBigDecimal(vo.getE2()));
        vo.setC31(formatBigDecimal(vo.getC31()));
        vo.setC32(formatBigDecimal(vo.getC32()));
        vo.setE31(formatBigDecimal(vo.getE31()));
        vo.setE32(formatBigDecimal(vo.getE32()));
        vo.setF3(formatBigDecimal(vo.getF3()));
        return vo;
    }

    /**
     * 查询沥青密度试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportLqMdsyjcBsInfoVo> queryPageList(ReportLqMdsyjcBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqMdsyjcBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqMdsyjcBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青密度试验检测记录列表
     */
    @Override
    public List<ReportLqMdsyjcBsInfoVo> queryList(ReportLqMdsyjcBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqMdsyjcBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqMdsyjcBsInfo> buildQueryWrapper(ReportLqMdsyjcBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqMdsyjcBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqMdsyjcBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqMdsyjcBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqMdsyjcBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqMdsyjcBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqMdsyjcBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqMdsyjcBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqMdsyjcBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqMdsyjcBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqMdsyjcBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqMdsyjcBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqMdsyjcBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqMdsyjcBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqMdsyjcBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqMdsyjcBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqMdsyjcBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqMdsyjcBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqMdsyjcBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqMdsyjcBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqMdsyjcBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqMdsyjcBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqMdsyjcBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportLqMdsyjcBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getCd()), ReportLqMdsyjcBsInfo::getCd, bo.getCd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), ReportLqMdsyjcBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getPih()), ReportLqMdsyjcBsInfo::getPih, bo.getPih());
        lqw.eq(StringUtils.isNotBlank(bo.getChucrq()), ReportLqMdsyjcBsInfo::getChucrq, bo.getChucrq());
        lqw.eq(bo.getA1() != null, ReportLqMdsyjcBsInfo::getA1, bo.getA1());
        lqw.eq(bo.getB1() != null, ReportLqMdsyjcBsInfo::getB1, bo.getB1());
        lqw.eq(bo.getC1() != null, ReportLqMdsyjcBsInfo::getC1, bo.getC1());
        lqw.eq(bo.getD1() != null, ReportLqMdsyjcBsInfo::getD1, bo.getD1());
        lqw.eq(bo.getE1() != null, ReportLqMdsyjcBsInfo::getE1, bo.getE1());
        lqw.eq(bo.getA2() != null, ReportLqMdsyjcBsInfo::getA2, bo.getA2());
        lqw.eq(bo.getB2() != null, ReportLqMdsyjcBsInfo::getB2, bo.getB2());
        lqw.eq(bo.getC2() != null, ReportLqMdsyjcBsInfo::getC2, bo.getC2());
        lqw.eq(bo.getD2() != null, ReportLqMdsyjcBsInfo::getD2, bo.getD2());
        lqw.eq(bo.getE2() != null, ReportLqMdsyjcBsInfo::getE2, bo.getE2());
        lqw.eq(bo.getA3() != null, ReportLqMdsyjcBsInfo::getA3, bo.getA3());
        lqw.eq(bo.getB3() != null, ReportLqMdsyjcBsInfo::getB3, bo.getB3());
        lqw.eq(bo.getC31() != null, ReportLqMdsyjcBsInfo::getC31, bo.getC31());
        lqw.eq(bo.getC32() != null, ReportLqMdsyjcBsInfo::getC32, bo.getC32());
        lqw.eq(bo.getD3() != null, ReportLqMdsyjcBsInfo::getD3, bo.getD3());
        lqw.eq(bo.getE31() != null, ReportLqMdsyjcBsInfo::getE31, bo.getE31());
        lqw.eq(bo.getE32() != null, ReportLqMdsyjcBsInfo::getE32, bo.getE32());
        lqw.eq(bo.getF3() != null, ReportLqMdsyjcBsInfo::getF3, bo.getF3());
        return lqw;
    }

    /**
     * 新增沥青密度试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportLqMdsyjcBsInfoBo bo) {
        ReportLqMdsyjcBsInfo add = BeanUtil.toBean(bo, ReportLqMdsyjcBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改沥青密度试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportLqMdsyjcBsInfoBo bo) {
        ReportLqMdsyjcBsInfo update = BeanUtil.toBean(bo, ReportLqMdsyjcBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqMdsyjcBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青密度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
